<template>
  <el-row :gutter="10">
    <el-col :xl="8" :lg="8" :md="24" :sm="24" :xs="24">
      <!-- 订单信息 -->
      <OrderBasicInfo :order-data="orderData">
        <!-- <template #tool>
          <div class="flex justify-end w-full">
            <el-button>打印小票 </el-button>
            <el-button type="primary">撤销 </el-button>
          </div>
        </template> -->
      </OrderBasicInfo>
    </el-col>
    <el-col :xl="8" :lg="8" :md="24" :sm="24" :xs="24">
      <!-- 会员信息 -->
      <OrderMemberInfo :order-data="orderData">
        <template #tool>
          <div class="w-full flex h-32px items-center justify-end">
            <Icon icon="ep:shop" :size="16" />
          </div>
        </template>
      </OrderMemberInfo>
    </el-col>
    <el-col :xl="8" :lg="8" :md="24" :sm="24" :xs="24">
      <OrderStatusInfo :order-data="orderData">
        <!-- <template #tool>
          <div class="w-full flex h-32px items-center justify-end">
            <el-button>电子合同</el-button>
          </div>
        </template> -->
      </OrderStatusInfo>
    </el-col>
  </el-row>
  <!-- 商品信息 -->
  <ContentWrap
    class="!mb-10px"
    v-if="
      (orderData?.orderReasons && orderData?.orderReasons.length) ||
      (orderData?.orderStoreReasons && orderData?.orderStoreReasons.length)
    "
  >
    <el-descriptions
      title="门店备注"
      :column="1"
      v-if="orderData?.orderStoreReasons && orderData?.orderStoreReasons.length"
    >
      <!-- 第一层 -->
      <el-descriptions-item>
        <el-text v-for="(item, index) in orderData?.orderStoreReasons" :key="index">
          {{ item.nodeName }} {{ item.creator.split(':')[1] }} (ID:{{ item.creator.split(':')[0] }})
          {{ formatDate(item.createTime) }}【<el-text type="primary">{{ item.reason }} </el-text>】
        </el-text>
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions
      title="总部备注"
      :column="1"
      v-if="orderData?.orderReasons && orderData?.orderReasons.length"
    >
      <!-- 第一层 -->
      <el-descriptions-item>
        <el-text v-for="(item, index) in orderData?.orderReasons" :key="index">
          {{ item.creator.split(':')[1] }} (ID:{{ item.creator.split(':')[0] }})
          {{ formatDate(item.createTime) }}【<el-text type="primary">{{ item.reason }} </el-text>】
        </el-text>
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>
  <OrderServiceInfo :goods-list="orderData.items" :order-data="orderData" />
  <OrderDeductInfo :goods-list="orderData.buckItems" />
  <!-- 服务员工 -->
  <!-- <ContentWrap class="!mb-10px" v-if="orderData?.employeeStr?.length > 0">
    <el-descriptions title="服务员工" :column="1">
      <el-descriptions-item>
        服务员工:
        <span v-for="item in orderData.employeeStr" :key="item" class="mr-5px"
          >{{ item.employeeName }}({{ item.employeeId }}):{{ item.proportion }}%</span
        >
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap> -->

  <ContentWrap class="!mb-10px">
    <!-- 操作日志 -->
    <el-descriptions title="订单日志">
      <el-descriptions-item labelClassName="no-colon">
        <el-timeline>
          <el-timeline-item
            v-for="saleLog in orderData.logs"
            :key="saleLog.id"
            :timestamp="formatDate(saleLog.createTime)"
            placement="top"
          >
            <div :class="[isDark ? 'el-timeline-right-content-dark' : 'el-timeline-right-content']">
              <span>{{ saleLog.content }}</span>
            </div>
            <template #dot>
              <span
                :style="{ backgroundColor: getUserTypeColor(saleLog.userType) }"
                class="dot-node-style"
              >
                {{ getDictLabel(DICT_TYPE.USER_TYPE, saleLog.userType)[0] || '系' }}
              </span>
            </template>
          </el-timeline-item>
        </el-timeline>
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>

  <!-- 各种操作的弹窗 -->
  <UpdateAuditReasonForm ref="updateAuditReasonFormRef" @success="getDetail" />
</template>
<script lang="ts" setup>
import * as TradeOrderApi from '@/api/mall/trade/order'

import { fenToYuan } from '@/utils'
import { DICT_TYPE, getDictLabel, getDictObj } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import UpdateAuditReasonForm from '@/views/mall/trade/afterSale/form/AfterSaleDisagreeForm.vue'

import { useTagsViewStore } from '@/store/modules/tagsView'
import { OrderMemberInfo, OrderStatusInfo } from '@/views/mall/trade/order/components'
import { OrderBasicInfo, OrderServiceInfo, OrderDeductInfo } from './../components/index'
import { useAppStore } from '@/store/modules/app'

const appStore = useAppStore()

const isDark = computed(() => appStore.getIsDark)
defineOptions({ name: 'TradeAfterSaleDetail' })

const message = useMessage() // 消息弹窗
const { params } = useRoute() // 查询参数
const { push, currentRoute } = useRouter() // 路由
const orderData = ref({
  order: {},
  logs: [],
  status: undefined,
  orderItem: {}
})
const updateAuditReasonFormRef = ref() // 拒绝售后表单 Ref

/** 获得 userType 颜色 */
const getUserTypeColor = (type: number) => {
  const dict = getDictObj(DICT_TYPE.USER_TYPE, type)
  switch (dict?.colorType) {
    case 'success':
      return '#67C23A'
    case 'info':
      return '#909399'
    case 'warning':
      return '#E6A23C'
    case 'danger':
      return '#F56C6C'
  }
  return '#165DFF'
}

/** 获得详情 */
const getDetail = async () => {
  const id = params.id as unknown as number
  if (id) {
    const res = await TradeOrderApi.getOrder(id)
    // 没有表单信息则关闭页面返回
    if (res == null) {
      message.notifyError('服务订单不存在')
      close()
    }
    orderData.value = res
  }
}

const { delView } = useTagsViewStore() // 视图操作
/** 关闭 tag */
const close = () => {
  delView(unref(currentRoute))
  push({ name: 'ServeOrder' })
}
onMounted(async () => {
  await getDetail()
})
</script>
<style lang="scss" scoped>
:deep(.el-descriptions) {
  &:not(:nth-child(1)) {
    margin-top: 20px;
  }

  .el-descriptions__title {
    display: flex;
    align-items: center;
  }

  .el-descriptions-item__container {
    margin: 0 10px;

    .no-colon {
      margin: 0;

      &::after {
        content: '';
      }
    }
  }
}

// 时间线样式调整
:deep(.el-timeline) {
  margin: 10px 0 0 160px;

  .el-timeline-item__wrapper {
    position: relative;
    top: -20px;

    .el-timeline-item__timestamp {
      position: absolute !important;
      top: 10px;
      left: -150px;
    }
  }

  .el-timeline-right-content {
    display: flex;
    align-items: center;
    min-height: 30px;
    padding: 10px;
    background-color: var(--app-content-bg-color);

    &::before {
      position: absolute;
      top: 10px;
      left: 13px;
      border-color: transparent var(--app-content-bg-color) transparent transparent; /* 尖角颜色，左侧朝向 */
      border-style: solid;
      border-width: 8px; /* 调整尖角大小 */
      content: '';
    }
    &-dark {
      display: flex;
      align-items: center;
      min-height: 30px;
      padding: 10px;
      border-radius: var(--el-card-border-radius);
      background-color: var(--el-bg-color);

      &::before {
        position: absolute;
        top: 10px;
        left: 13px; /* 将伪元素水平居中 */
        border-color: transparent var(--el-bg-color) transparent transparent; /* 尖角颜色，左侧朝向 */
        border-style: solid;
        border-width: 8px; /* 调整尖角大小 */
        content: ''; /* 必须设置 content 属性 */
      }
    }
  }

  .dot-node-style {
    position: absolute;
    left: -5px;
    display: flex;
    width: 20px;
    height: 20px;
    font-size: 10px;
    color: #fff;
    border-radius: 50%;
    justify-content: center;
    align-items: center;
  }
}
</style>
